package cn.stylefeng.guns.modular.yingbao.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.yingbao.entity.SysProtocol;
import cn.stylefeng.guns.modular.yingbao.mapper.SysProtocolMapper;
import cn.stylefeng.guns.modular.yingbao.model.params.SysProtocolParam;
import cn.stylefeng.guns.modular.yingbao.model.result.SysProtocolResult;
import  cn.stylefeng.guns.modular.yingbao.service.SysProtocolService;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 协议设置 服务实现类
 * </p>
 *
 * @author jj
 * @since 2020-01-10
 */
@Service
public class SysProtocolServiceImpl extends ServiceImpl<SysProtocolMapper, SysProtocol> implements SysProtocolService {

    @Override
    public void add(SysProtocolParam param){
        SysProtocol entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(SysProtocolParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(SysProtocolParam param){
        SysProtocol oldEntity = getOldEntity(param);
        SysProtocol newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public SysProtocolResult findBySpec(SysProtocolParam param){
        return null;
    }

    @Override
    public List<SysProtocolResult> findListBySpec(SysProtocolParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(SysProtocolParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public ResponseData queryByType(Integer type) {
        if(type == null){
            return ResponseData.error(500,"参数不能为空");
        }
        SysProtocol type1 = this.baseMapper.selectOne(new QueryWrapper<SysProtocol>().eq("type", type));
        if(type1 != null){
            return ResponseData.success(type1);
        }
        return ResponseData.error(500,"暂无信息");
    }

    private Serializable getKey(SysProtocolParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private SysProtocol getOldEntity(SysProtocolParam param) {
        return this.getById(getKey(param));
    }

    private SysProtocol getEntity(SysProtocolParam param) {
        SysProtocol entity = new SysProtocol();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
